package org.jeecg.modules.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.business.entity.BusDict;
import org.jeecg.modules.business.mapper.BusDictMapper;
import org.jeecg.modules.business.model.BusDictTreeModel;
import org.jeecg.modules.business.service.IBusDictService;
import org.jeecg.modules.business.util.FindsBusDictsChildrenUtil;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.model.SysDepartTreeModel;
import org.jeecg.modules.system.util.FindsDepartsChildrenUtil;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 业务字典表
 * @Author: jeecg-boot
 * @Date:   2023-03-06
 * @Version: V1.0
 */
@Service
public class BusDictServiceImpl extends ServiceImpl<BusDictMapper, BusDict> implements IBusDictService {
    /**
     * queryTreeList 对应 queryTreeList 查询所有的部门数据,以树结构形式响应给前端
     */
    @Override
    public List<BusDictTreeModel> queryTreeList(String hotelId) {
        LambdaQueryWrapper<BusDict> query = new LambdaQueryWrapper<BusDict>();
        query.eq(BusDict::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
        query.eq(BusDict::getHotelId, hotelId);
//        query.orderByAsc(BusDict::getCreateTime);
        List<BusDict> list = this.list(query);
        //update-begin---author:wangshuai ---date:20220307  for：[JTC-119]在部门管理菜单下设置部门负责人 创建用户的时候不需要处理
        //设置用户id,让前台显示
//        this.setUserIdsByDepList(list);
        //update-begin---author:wangshuai ---date:20220307  for：[JTC-119]在部门管理菜单下设置部门负责人 创建用户的时候不需要处理
        // 调用wrapTreeDataToTreeList方法生成树状数据
        List<BusDictTreeModel> listResult = FindsBusDictsChildrenUtil.wrapTreeDataToTreeList(list);
        return listResult;
    }

    /**
     * queryTreeList 根据部门id查询,前端回显调用
     */
    @Override
    public List<BusDictTreeModel> queryTreeList(String ids,String hotelId) {
        List<BusDictTreeModel> listResult=new ArrayList<>();
        LambdaQueryWrapper<BusDict> query = new LambdaQueryWrapper<BusDict>();
        query.eq(BusDict::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
        query.eq(BusDict::getHotelId, hotelId);
        if(oConvertUtils.isNotEmpty(ids)){
            query.in(true,BusDict::getId,ids.split(","));
        }
//        query.orderByAsc(BusDict::getCreateTime);
        List<BusDict> list= this.list(query);
        for (BusDict depart : list) {
            listResult.add(new BusDictTreeModel(depart));
        }
        return  listResult;

    }
}
